/**
 *
 */
class Protocol {

    constructor() {
        this.result = [];
        this.end = "";
        this.data = {
            state: true,
            res: {
                error: false,
                data: null,
            }
        };
    }

    /**
     *
     * @param params
     */
    encode(...params) {
        const length = params.length;

        let parameter;

        let request = `*${length}\r\n`;

        for (let i = 0; i < length; i++) {
            parameter = params[i];
            if (typeof parameter === "string") {
                request += `$${Buffer.byteLength(parameter)}\r\n${parameter}\r\n`;
            } else if (typeof parameter === "number") {
                parameter = parameter.toString();
                request += `$${Buffer.byteLength(parameter)}\r\n${parameter}\r\n`;
            } else {
                throw new Error("encode ags err");
            }
        }
        return request;
    }

    /**
     *
     * @param data {Buffer}
     */
    write(data) {
        const array = (this.end + data.toString()).split("\r\n");
        this.end = array.pop();
        this.result = this.result.concat(array);
    }


}

module.exports = Protocol;
